/**
 * @author CCPalu
 */

function graphicAreaVariables(){
	var cnv = document.getElementById('CNV');
	var graphic = document.createElement('OBJECT');
	document.body.appendChild(graphic);
	
	graphic.name='graphicVariables';
	graphic.id='graphicVariables';

	graphic.samples = function(){
		return cnv.segData[cnv.ch[0]].length;
	};
	graphic.segmentWidth=12;
	graphic.segmentHeight=6;
	
	graphic.top = 360;
	graphic.generalLeft = 280;
	graphic.generalHeight = function(){
		return this.segmentHeight * this.samples();
	};
	
	graphic.labelWidth = 45;
	graphic.labelLeft = function(){
		return this.generalLeft;
	};
	graphic.CNVWidth = function(){
		return 600 + ((cnv.ch.length - 1) * (3400 / 21));
	};
	graphic.CNVLeft = function(){
		return this.labelLeft()+this.labelWidth - 17 ;
	};
	graphic.distanceWidth = 180;
	graphic.distanceLeft = function(){
		return this.CNVLeft()+this.CNVWidth();//-19;
	};
	graphic.clinicalWidth = function(){
		return cnv.clinical.labels.length*this.segmentWidth*1.3;
	};
	graphic.clinicalLeft = function(){
		return this.distanceLeft()+this.distanceWidth+25*1;
	};
	
	return false;
}

function distanceChart(dist) {
	var cnv = document.getElementById('CNV');
	var ch = cnv.ch;
	var graphicVar = document.getElementById('graphicVariables');
	
	remove_element_by_id("simplechart");
	remove_element_by_id("node2");
	remove_element_by_id("chartLabel");
	remove_element_by_id("labelLegend");
	remove_element_by_id("distanceLegend");
	remove_element_by_id("copyNumber");
	
	if (document.getElementById('CNV').similarity.distance!=undefined) {
		remove_element_by_id("chrefDiv");
	}
	
	cnv.similarity.distance=	dist[0];
	cnv.similarity.sampleName=	dist[2];
	cnv.similarity.sampleNumber=	dist[1];
	var	method=document.getElementById('sim_method');
	cnv.similarity.metric=[method.options[method.selectedIndex].innerHTML, method.value];
	var item = document.getElementById("choose_sample");
	cnv.similarity.sample = [item.options[item.selectedIndex].innerHTML,item.value];
	cnv.similarity.chromosome= ch;
	if (document.getElementById('sim_method').selectedIndex){
		var distanceLegend=document.createElement('IMG');
		distanceLegend.id='distanceLegend';
		distanceLegend.src='http://cnviewer.googlecode.com/hg/Images/distance.png';
		document.getElementById('chartingArea').appendChild(distanceLegend);
		setPosition(distanceLegend, graphicVar.distanceWidth*1 + graphicVar.distanceLeft()*1, (graphicVar.top*1 + graphicVar.generalHeight()*1)/2-15);
		distanceLegend.style.zIndex=-16;
	}
	
	clinicalMap(dist[1].reverse());
	cnvMap(dist[1], dist[2].reverse(), dist[0].reverse());
	
	return false;	
}

function createCanvas(w,h, id, parent){
	if (parent) {
		var canvas = document.createElement("canvas");
		canvas.id = id;
		if (typeof parent == 'string'){
			parent=document.getElementById('parent');
		}
		parent.appendChild(canvas);
	}
	else {
		remove_element_by_id(id);
		var canvas = document.createElement("canvas");
		canvas.id = id;
		document.getElementById('chartingArea').appendChild(canvas);
	}
	canvas.width=w;
	canvas.height=h;
	canvas.style.zIndex=-10;
	
	return canvas;
}
